package androidx.gdata.spreadsheets.xml;

import org.w3c.dom.Node;

import java.util.List;

import androidx.gdata.Author;
import androidx.gdata.Category;
import androidx.gdata.LinkUrl;
import androidx.gdata.spreadsheets.Spreadsheet;
import androidx.gdata.spreadsheets.Worksheet;
import androidx.gdata.spreadsheets.WorksheetContent;
import androidx.gdata.xml.AuthorDeserializer;
import androidx.gdata.xml.CategoryDeserializer;
import androidx.gdata.xml.ThingWithLinksDeserializer;

public class WorksheetDeserializer
  extends ThingWithLinksDeserializer<Worksheet> {
  
  private static CategoryDeserializer categoryBuilder = 
    new CategoryDeserializer();
 
  @Override
  public Worksheet build(Node entryNode) {
    node = entryNode;
    String id = getText("id");
    String updated = getText("updated");
    Category[] categories = null;
    List<Node> categoryNodes = getChildNodes("category");
    if (categoryNodes != null) {
      categories = new Category[categoryNodes.size()];
      int i = 0;
      for (Node categoryNode : categoryNodes) {
        categories[i++] = categoryBuilder.build(categoryNode);
      }
    }
    
    String title = getText("title");
    Node contentNode = getChildNode("content");
    WorksheetContent content = new WorksheetContent(
        "text", contentNode.getNodeValue());
    LinkUrl[] links = getLinks();
    return new Worksheet(id, updated, title, content, links);
  }

}
